<template>
  <div class="billRecordBox">
    <div class="headerBtns">
      <el-button size="small" type="primary" round  @click="save">发送ERP审核</el-button>
      <el-button size="small" style="margin-left:100px " type="primary" round  @click="getErpResult">获取ERP审核结果</el-button>
    </div>
    <div class="tableBox">
      <el-table
          highlight-current-row
          :data="billRecords"
          style="width: 100%">
        <el-table-column
            fixed="left"
            type="index"
            width="50">
        </el-table-column>
        <el-table-column
            fixed="left"
            prop="stockMasterCode"
            label="盘点任务号"
            >
        </el-table-column>
        <el-table-column
            prop="itemCode"
            label="物料编码"
            >
        </el-table-column>
        <el-table-column
                prop="itemName"
                label="物料名称"
               >
        </el-table-column>
        <el-table-column
                prop="itemPc"
                label="物料批次"
        ></el-table-column>
        <el-table-column
                label="盘盈(+)/盘亏(-)"
        >
          <template slot-scope="scope">
            <div>{{scope.row.residualQuantity}}</div>
          </template>
        </el-table-column>
        <el-table-column
                prop="quantity"
                label="库存数量"
        ></el-table-column>
        <el-table-column
                prop="taskQuantity"
                label="实盘数量"
        >
        </el-table-column>
        <el-table-column
                prop="taskTime"
                label="盘点时间"
        >
        </el-table-column>
      </el-table>
      <el-pagination
              style="text-align: right;height: 30px;"
              class="pagination-bottom"
              @current-change="handleCurrentChange"
              @size-change="handleSizeChange"
              :page-sizes="[10, 20, 30, 50]"
              :current-page.sync="searchData.pageNum"
              :page-size="searchData.pageSize"
              layout="sizes, total, prev, pager, next, jumper"
              :total="searchData.total"
      >
      </el-pagination>
    </div>

  </div>
</template>

<script>
  import {mapState} from "vuex";

  export default{
    name: 'BillRecordBox',
    props: [
      "masterData",
      "activeName"
    ],
    data(){
      return {
        billRecords: [],
        profitList:[],
        lossList:[],
        stockMasterCode:'',
        stockMasterId:'',
        wareId: PF.getLocal("wareId", "number"),
        state: this.$route.params.state,
        searchData: {
          pageNum: 1,
          pageSize: 10,
          stockMasterCode: '',
          wareId: PF.getLocal("wareId", "number"),
        },
      }
    },
    computed: {
      masterBillNo(){
        return this.masterData
      },
      ...mapState([
        "recordTypes"
      ])
    },
    watch: {
      activeName(val){
        if(val === 'third'){
          if(PF.isNull(val)){
            this.getBillRecordData()
          }else{
            IOT.tips('未获取到单号！', 'error');
          }
        }
      }
    },
    methods: {
      save(){
        if (this.state == 0){
          IOT.tips( '盘点任务未完成，请先完成盘点任务！', 'error');
        } else {
          if (this.profitList.length>0 || this.lossList.length>0){
            /*let stockProfitLossDto = {
              profitList:this.profitList,
              lossList:this.lossList,
              stockMasterCode:this.stockMasterCode,
              wareId:this.wareId,
              stockMasterId:this.stockMasterId
            }*/
            IOT.getServerData('/stock/take/log/inventoryReturn', 'get', {wareId:this.wareId,stockMasterCode:this.searchData.stockMasterCode}, (ret) => {
              console.log("生成盈亏单返回：",ret)
              if (ret.code === 200) {
                IOT.tips('创建成功！', 'success');
              } else {
                IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
              }
            });
          }else{
            IOT.tips( '没有盘点差异', 'error');
          }
        }

      },
      getErpResult(){
        if (this.profitList.length>0 || this.lossList.length>0){
          IOT.getServerData('/accept/log/selectInventory', 'post', {wareId:this.wareId,erpNo:this.searchData.stockMasterCode}, (ret) => {
            console.log("查询erp审核结果返回：",ret)
            if (ret.code === 200) {
              IOT.tips(ret.message, 'success');
            } else {
              IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
            }
          });
        }else{
          IOT.tips( '没有盘点差异', 'error');
        }
      },
      getBillRecordData() {
        this.searchData.stockMasterCode = this.masterData,
        this.stockMasterCode = this.masterData,
        IOT.getServerData('/stock/take/log/findResidualList', 'get', this.searchData, (ret) => {
          console.log("查询盘点差异返回：",ret)
          if (ret.code === 200) {
            let list = ret.rows;
            for(let i=0;i<list.length;i++){
                if (list[i].profitOrLoss == 1){
                  this.profitList.push(list[i]);
                  this.stockMasterId = list[i].stockMasterId;
                }else if(list[i].profitOrLoss == -1){
                  this.lossList.push(list[i]);
                  this.stockMasterid = list[i].stockMasterid;
                }
            }
            console.log("profitList:",this.profitList)
            console.log("lossList:",this.lossList)
            this.billRecords = list;
            this.searchData.total = ret.total;
            this.searchData.pageNum = ret.pageNumber
          } else {
            IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
          }
        });
      },
      handleCurrentChange(val) {
        this.pageNum = val;
        this.getBillRecordData()
      },
      handleSizeChange(val) {
        this.searchData.pageSize = val;
        this.getBillRecordData();
      },
    },
    created() {
    },
    mounted() {
    }
  }
</script>
